package com.thd.springboottest.camunda.controller;

import com.thd.springboottest.camunda.service.WorkFlowService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.runtime.ProcessInstanceModificationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashSet;
import java.util.Set;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 *
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda/jump")
public class Camunda10jump {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;
    /**
     * 部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/jump/deploy
    @RequestMapping(value="/deploy",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deploy(){
        String deployId = this.workFlowService.deploy("processes/10jump.bpmn");
        System.out.println("deployId:" + deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 开启流程
     */
    // url : http://127.0.0.1:8899/thd/camunda/jump/startProcess/[businessKey:gateway001]
    @RequestMapping(value="/startProcess/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> startProcess(@PathVariable String businessKey){
        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("jump",businessKey);
       System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }

    /*
    查询待办
    http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/a01
    taskA
     */
    /**
     * 自由跳转
     */
    // url : http://127.0.0.1:8899/thd/camunda/jump/jump/[businessKey:gateway001]/taskE
    @RequestMapping(value="/jump/{businessKey}/{targetActivityId}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> jump(@PathVariable String businessKey,@PathVariable String targetActivityId ){

        ProcessInstance pi = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
        ProcessInstanceModificationBuilder processInstanceModificationBuilder = processEngine.getRuntimeService().createProcessInstanceModification(pi.getId());

        Set<String> activityIdSet = new HashSet<>();

        processEngine.getTaskService().createTaskQuery().processInstanceId(pi.getId()).active().list().forEach(taskQuery -> {
            String activityId = taskQuery.getTaskDefinitionKey();
            if(activityIdSet.add(activityId)){
                processInstanceModificationBuilder.cancelAllForActivity(activityId);
            }
        });
        processInstanceModificationBuilder.startBeforeActivity(targetActivityId)
                .setVariable("name","devil13th")
                .execute();

        System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }

    /*
    查询待办
    发现已经跳转至taskE
     */
}
